import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;

import com.sun.opengl.util.GLUT;


public class Grid {
	GL gl;
	GLU glu;
	GLUT glut;
	int COUNT_DOT=11;
	int DIST_BETWEEN_FIELDS = 160;
	int ZOOM=8;
	private int list_id;//variable for static models
	Vector3D mass[][]=new Vector3D[COUNT_DOT][COUNT_DOT];
	
	public Grid(GL gl_, GLU glu_){
		gl=gl_;
		glu=glu_;
		glut = new GLUT();
		makeMass();
	}
	
	private void makeMass(){
		for(int i=0; i<COUNT_DOT; ++i){
			for(int j=0; j<COUNT_DOT; ++j)
			{
				mass[i][j]=new Vector3D();
				mass[i][j].setXYZ((float)i*ZOOM, (float)j*ZOOM, 1f);
			}
		}
	}
	
	public void draw(){
		gl.glActiveTexture(GL.GL_TEXTURE0);
		gl.glDisable(GL.GL_TEXTURE_2D);
		gl.glColor3f(1.0f, 0.0f, 0.0f);
		if(list_id != 0){			 
		    gl.glCallList(list_id);
		 }
		 else {
			 list_id=gl.glGenLists(1);
			 gl.glNewList(list_id, GL.GL_COMPILE_AND_EXECUTE);
		gl.glBegin(GL.GL_LINES);
		//first field
		for(int i=0; i<COUNT_DOT-1; ++i)
		{
			for(int j=0; j<COUNT_DOT-1; ++j)
			{
				gl.glVertex3f(mass[i][j].x(), 
						mass[i][j].y(), mass[i][j].z());
				gl.glVertex3f(mass[i+1][j].x(), 
						mass[i+1][j].y(), mass[i+1][j].z());
				
				gl.glVertex3f(mass[i][j].x(), 
						mass[i][j].y(), mass[i][j].z());
				gl.glVertex3f(mass[i][j+1].x(), 
						mass[i][j+1].y(), mass[i][j+1].z());
			}
			gl.glVertex3f(mass[i][COUNT_DOT-1].x(), 
					mass[i][COUNT_DOT-1].y(),
					mass[i][COUNT_DOT-1].z());
			gl.glVertex3f(mass[i+1][COUNT_DOT-1].x(), 
					mass[i+1][COUNT_DOT-1].y(),
					mass[i+1][COUNT_DOT-1].z());
		}
		for(int j=0; j<COUNT_DOT-1; ++j)
		{
			gl.glVertex3f(mass[COUNT_DOT-1][j].x(), 
					mass[COUNT_DOT-1][j].y(), mass[COUNT_DOT-1][j].z());
			gl.glVertex3f(mass[COUNT_DOT-1][j+1].x(), 
					mass[COUNT_DOT-1][j+1].y(), mass[COUNT_DOT-1][j+1].z());
			}
				
		//second field
		for(int i=0; i<COUNT_DOT-1; ++i)
		{
			for(int j=0; j<COUNT_DOT-1; ++j)
			{
				gl.glVertex3f(mass[i][j].x(), 
						mass[i][j].y()+DIST_BETWEEN_FIELDS, mass[i][j].z());
				gl.glVertex3f(mass[i+1][j].x(), 
						mass[i+1][j].y()+DIST_BETWEEN_FIELDS, mass[i+1][j].z());
				
				gl.glVertex3f(mass[i][j].x(), 
						mass[i][j].y()+DIST_BETWEEN_FIELDS, mass[i][j].z());
				gl.glVertex3f(mass[i][j+1].x(), 
						mass[i][j+1].y()+DIST_BETWEEN_FIELDS, mass[i][j+1].z());
			}
			gl.glVertex3f(mass[i][COUNT_DOT-1].x(), 
					mass[i][COUNT_DOT-1].y()+DIST_BETWEEN_FIELDS,
					mass[i][COUNT_DOT-1].z());
			gl.glVertex3f(mass[i+1][COUNT_DOT-1].x(), 
					mass[i+1][COUNT_DOT-1].y()+DIST_BETWEEN_FIELDS,
					mass[i+1][COUNT_DOT-1].z());
		}
		for(int j=0; j<COUNT_DOT-1; ++j)
		{
			gl.glVertex3f(mass[COUNT_DOT-1][j].x(), 
					mass[COUNT_DOT-1][j].y()+DIST_BETWEEN_FIELDS, mass[COUNT_DOT-1][j].z());
			gl.glVertex3f(mass[COUNT_DOT-1][j+1].x(), 
					mass[COUNT_DOT-1][j+1].y()+DIST_BETWEEN_FIELDS, mass[COUNT_DOT-1][j+1].z());
			}
		gl.glEnd();
		gl.glColor3f (1,1,1);
		//first field
		for(int i = 0; i < 10; i++) {
			gl.glRasterPos3f(-4, 4+i*8,2);
			char string[] = new char[2];
			string[0] = (char)((int)'a' + (int)i);
			glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, string[0]);
			gl.glRasterPos3f(84, 4+i*8,2);
			glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, string[0]);
		}
		for(int i = 0; i < 10; i++) {
			gl.glRasterPos3f(4+i*8, -4,2);
			char string[] = new char[2];
			string[0] = (char)((int)'0' + (int)i);
			glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, string[0]);
			gl.glRasterPos3f(4+i*8, 84,2);
			glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, string[0]);
		}		
		//second field
		for(int i = 0; i < 10; i++) {
			gl.glRasterPos3f(-4, DIST_BETWEEN_FIELDS+4+i*8,2);
			char string[] = new char[2];
			string[0] = (char)((int)'a' + (int)i);
			glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, string[0]);
			gl.glRasterPos3f(84, DIST_BETWEEN_FIELDS+4+i*8,2);
			glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, string[0]);
		}
		for(int i = 0; i < 10; i++) {
			gl.glRasterPos3f(4+i*8, -4+DIST_BETWEEN_FIELDS,2);
			char string[] = new char[2];
			string[0] = (char)((int)'0' + (int)i);
			glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, string[0]);
			gl.glRasterPos3f(4+i*8, 84+DIST_BETWEEN_FIELDS,2);
			glut.glutBitmapCharacter(GLUT.BITMAP_HELVETICA_18, string[0]);
		}
		gl.glEndList();
		}
		gl.glEnable(GL.GL_TEXTURE_2D);
	}
	
}	